package org.anderground.app.view.dialogs;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.anderground.app.R;
import org.anderground.core.drawer.MetroDrawer;

import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;

/**
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 */
public class ClassesDialog extends BaseDialog implements
		ExpandableListView.OnChildClickListener,
		ExpandableListView.OnGroupClickListener,
		DialogInterface.OnClickListener {

	private static final String NAME_KEY = "Name";

	private static final String DESCRIPTION_KEY = "Description";

	private static final String MODULE_KEY = "module";

	/** the subscriber that would be notified about most of events */
	protected Listener listener;

	/** View to attach to the dialog */
	private ExpandableListView listView;

	private List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();

	private List<List<Map<String, Object>>> childData = new ArrayList<List<Map<String, Object>>>();

	private SimpleExpandableListAdapter listAdapter;

	/**
	 * public constructor
	 * 
	 * @param context
	 */
	public ClassesDialog(Context context) {
		super(context);

		this.listView = new ExpandableListView(context);
		this.listView.setOnChildClickListener(this);
		this.listView.setOnGroupClickListener(this);

		setView(listView);
		setNegativeButton(R.string.cancel, this);
		setPositiveButton(R.string.get_from_internet, this);
	}

	/**
	 * 
	 * @param items
	 */
	public void show(List<String> items) {
		String[] from = new String[] { NAME_KEY, DESCRIPTION_KEY };
		int[] to = new int[] { android.R.id.text1, android.R.id.text2 };

		this.listAdapter = new SimpleExpandableListAdapter(getContext(),
				groupData, R.layout.class_row, from, to, childData,
				R.layout.class_row, from, to);

		this.listView.setAdapter(this.listAdapter);

		this.groupData.clear();
		this.childData.clear();

		for (String item : items) {
			Map<String, String> curGroupMap = new HashMap<String, String>();
			this.groupData.add(curGroupMap);
			curGroupMap.put(NAME_KEY, item);
			curGroupMap.put(DESCRIPTION_KEY, "file has not opened yet");

			ArrayList<Map<String, Object>> children = new ArrayList<Map<String, Object>>();
			HashMap<String, Object> map = new HashMap<String, Object>();
			children.add(map);
			map.put(NAME_KEY, "Reading file");
			map.put(DESCRIPTION_KEY, "Please wait...");
			this.childData.add(children);
		}
		show();
	}

	/**
	 * called when the list of item's children has retrieved
	 * 
	 * @param item
	 * @param pack
	 */
	public void onChildrenRetrieved(String item, List<Class<?>> pack) {
		int i;
		for (i = 0; i < this.groupData.size(); i++) {
			Map<String, String> map = this.groupData.get(i);
			if (item.equals(map.get(NAME_KEY))) {
				break;
			}
		}
		List<Map<String, Object>> children = this.childData.get(i);
		Map<String, String> group = this.groupData.get(i);
		if (children != null && group != null) {
			children.clear();
			if (pack != null) {
				for (Class<?> module : pack) {
					if (MetroDrawer.class.isAssignableFrom(module)) {
						HashMap<String, Object> map = new HashMap<String, Object>();
						map.put(NAME_KEY, module.getSimpleName());
						map.put(DESCRIPTION_KEY, "");
						map.put(MODULE_KEY, module);
						children.add(map);
					}
				}
				group.put(DESCRIPTION_KEY, "");

			} else {
				group.put(DESCRIPTION_KEY, "Error opening file");
			}
			this.listAdapter.notifyDataSetChanged();
		}
	}

	public boolean onGroupClick(ExpandableListView parent, View v,
			int groupPosition, long id) {
		List<Map<String, Object>> children = this.childData.get(groupPosition);
		children.clear();
		HashMap<String, Object> map = new HashMap<String, Object>();
		children.add(map);
		map.put(NAME_KEY, "Reading file");
		map.put(DESCRIPTION_KEY, "Please wait...");
		if (this.listener != null) {
			String groupName = (String) this.groupData.get(groupPosition).get(
					NAME_KEY);
			this.listener.onGroupSelected(this, groupName);
		}
		return false;
	}

	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		if (this.listener != null) {
			Class<?> module = (Class<?>) this.childData.get(groupPosition).get(
					childPosition).get(MODULE_KEY);
			this.listener.onChildSelected(this, module);
		}
		hide();
		return true;
	}

	public void onClick(DialogInterface dialog, int button) {
		if (dialog == this.getDialog()) {
			switch (button) {
			case DialogInterface.BUTTON1:
				((Listener) listener).onDownloadButtonClick(this);
				break;

			default:
				break;
			}
		}
	}

	/**
	 * subscribe to notifications
	 * 
	 * @param listener
	 *            the listener
	 */
	public void setListener(Listener listener) {
		if (listener != null) {
			this.listener = listener;
		}
	}

	/**
	 * Listener interface.
	 * 
	 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
	 */
	public interface Listener {

		public void onGroupSelected(ClassesDialog itemsDialog, String groupName);

		public void onChildSelected(ClassesDialog itemsDialog, Class<?> module);

		public void onDownloadButtonClick(BaseDialog dialog);

	}

}
